package c.x.jy.bean.event.listener;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import c.x.jy.bean.event.*;
import c.x.jy.dao.dto.TeacherEntityDto;
import c.x.jy.dao.entity.*;
import c.x.jy.service.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

import c.x.jy.common.utils.DateUtils;
import c.x.jy.dao.model.OrderForm;
import c.x.jy.service.config.BusinessConfigService;
import c.x.jy.service.config.WechatConfigService;
import c.x.jy.service.redis.Cache;
import c.x.jy.service.redis.RedisConstant;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class OrderSuccessListener {

	@Autowired
	private WechatConfigService wechatConfigService;

	@Autowired
	private Cache redisUtils;

	@Autowired
	private MemberService memberService;

	@Autowired
	private OrderBillService orderBillService;
	
	@Autowired
	private OrderService orderService;

	@Autowired
	private OrderItemsService orderItemsService;
	
	@Autowired
	private c.x.jy.service.service.TeacherServiceService teacherServiceService;
	
 
	@Autowired
	private BirthService birthService;
	
	@Autowired
	private TeacherConfigService tacherConfigService;

	@Autowired
	private QuicktestService quicktestService;
	
	@Autowired
	private RechargeService rechargeService;
	@Autowired
	private MemberWalletService memberWalletService;
	@Autowired
	private MemberRechargeRecordService memberRechargeRecordService;
	@Autowired
	private BusinessConfigService businessConfigService;
	
	@Autowired
	private TeacherService teacherService;
	@Autowired
	private	BusinessService 	businessService;
	
	/**
	 * 咨询订单 释放分布式设备锁，更新订单日志 更新订单信息 ,开锁 ，发送短信
	 * 
	 * @param orderDoneEvent
	 */
	@Async
	@EventListener
	public void payServiceSuccess(OrderDoneEvent orderDoneEvent) {
		log.info(JSON.toJSONString(orderDoneEvent));
		log.info("订单支付成功监听");
		OrderBillEntity order = (OrderBillEntity) orderDoneEvent.getData();
		order = orderBillService.selectByorderForUpdate(order.getId());
		if (!order.getStatus().equals(0)) {
			log.info("该订单支付支付状态不符!");
			return;
		}
		OrderEntity o = orderService.selectOrderByNo(order.getOrderNo());
		o=orderService.selectOrderBYIdForUpdate(o.getId());
		// 订单类型，1-咨询订单，2-闪测订单，3-提问订单，4-充值订单
		if (o ==null||!o.getServiceType().equals(OrderForm.orderType.consult.getType())) {
			log.info("非订单咨询订单 退出!");
			return;
		}
       Map<String, Object> map=  orderDoneEvent.getMap();
       order.setTransNumber(map.get("transaction_id").toString());
       order.setActualPrice(new BigDecimal(map.get("cash_fee").toString()).divide(new BigDecimal(100)));
       order.setStatus(1);
       orderBillService.updateById(order);
       
       o.setTotalAmount(order.getActualPrice());
       o.setStatus(1);
       orderService.updateById(o);
 
       List <OrderItemsEntity> list=     	orderItemsService.selectListByOrderNo(o.getId());
        list.forEach(item->{
	        TeacherServiceEntity tearchService = teacherServiceService.selectTezcherServiceForUpdate(item.getItemId());
	        tearchService.setTestPeoplenum((tearchService.getTestPeoplenum()==null?0:tearchService.getTestPeoplenum())+1);
	    	teacherServiceService.updateById(tearchService);
        });
       o.setItemlist(list);
       o.setOrderBill(order);
       OrderCommissionEvent oc=new OrderCommissionEvent(this,o);
       EventPublisher.publishEvent(oc);

		/**
		 * {{first.DATA}}
		 * 服务单号：{{keyword1.DATA}}
		 * 咨询主题：{{keyword2.DATA}}
		 * 咨询内容：{{keyword3.DATA}}
		 * {{remark.DATA}}
		 */
		//发送公众号模板消息
       String templateId=businessConfigService.getBusinessConfigValue(null, "GZH_NOTE_TEMPLATE");
        BusinessEntity business=  businessService.getById( order.getBusinessId());
		String url="wechat/topage?page=page/order/orderlist/orderlist&bcode="+business.getCode();
		TeacherEntityDto teacher=		teacherService.selectById(o.getTeacherId());
       // MemberEntity member=		memberService.getById(order.getMemberId());
		List<String> listmsg=new ArrayList<>();
		
		listmsg.add("订单服务进度通知");
		listmsg.add( teacher.getNickname()+":您收到新的咨询订单！"  );
		
		listmsg.add(order.getOrderNo());
		listmsg.add(list.get(0).getItemName());
		listmsg.add(list.get(0).getDescription());



		EventPublisher.publishEvent(new WxTemplateMsgEvent(this,  memberService.getById(teacher.getMemberId()).getOpenid(),templateId,url,listmsg));
       
   	// 释放锁
		redisUtils.remove(RedisConstant.REDISLOCK_KEY, o.getOrderNo() + ":" + o.getOrderNo() );
		
		redisUtils.remove(RedisConstant.REDISLOCK_KEY, o.getOrderNo() );
       
 
       
       
	}

	/**
	 * 闪测订单
	 * 
	 * @param orderDoneEvent
	 */
	@Async
	@EventListener
	public void payquickServiceSuccess(OrderDoneEvent orderDoneEvent) {
		log.info(JSON.toJSONString(orderDoneEvent));
		log.info("订单支付成功监听");
		OrderBillEntity order = (OrderBillEntity) orderDoneEvent.getData();
		order = orderBillService.selectByorderForUpdate(order.getId());
		if (!order.getStatus().equals(0)) {
			log.info("该订单支付支付状态不符!");
			return;
		}
		OrderEntity o = orderService.selectOrderByNo(order.getOrderNo());
		o=orderService.selectOrderBYIdForUpdate(o.getId());
		// 订单类型，1-咨询订单，2-闪测订单，3-提问订单，4-充值订单
		if (!o.getServiceType().equals(OrderForm.orderType.quick.getType())) {
			log.info("非闪测订单 退出!");
			return;
		}
		 Map<String, Object> map=  orderDoneEvent.getMap();
	       order.setTransNumber(map.get("transaction_id").toString());
	       order.setActualPrice(new BigDecimal(map.get("cash_fee").toString()).divide(new BigDecimal(100)));
	       order.setStatus(1);
			order.setPayDatetime(new Date());
	       orderBillService.updateById(order);
	       
	       o.setTotalAmount(order.getActualPrice());
	       o.setStatus(4);
		o.setPayDatetime(new Date());
	       orderService.updateById(o);
	 
	       List <OrderItemsEntity> list=     	orderItemsService.selectListByOrderNo(o.getId());
	      list.forEach(item->{
	    	 
	    	QuicktestEntity quick=  quicktestService.selectQuickTestByidForUPdate(item.getItemId());
	    	quick.setTestPeoplenum((quick.getTestPeoplenum()==null?0:quick.getTestPeoplenum())+1);
	    	quicktestService.updateById(quick);
	      });
	       o.setItemlist(list);
	       o.setOrderBill(order);
	       OrderCommissionEvent oc=new OrderCommissionEvent(this,o);
	       EventPublisher.publishEvent(oc);
	       

	       List <TeacherEntity>teacherlist=  teacherService.selectTeachersByJoinQuickAsk();
	      BusinessEntity business=  businessService.getById( order.getBusinessId());
	       if(teacherlist!=null&&!teacherlist.isEmpty())
	       for (TeacherEntity teacherEntity : teacherlist) {
	    	  if(!o.getMemberId().equals(teacherEntity.getMemberId())){
				  String templateId=businessConfigService.getBusinessConfigValue(null, "GZH_NOTE_TEMPLATE");
				  String url="wechat/topage?page=page/teacher/teacherindex/teacherindex&bcode="+business.getCode();
				  TeacherEntityDto teacher=		teacherService.selectById(teacherEntity.getId());
				  //	MemberEntity member=		memberService.getById(order.getMemberId());
				  List<String> listmsg=new ArrayList<>();

				  listmsg.add("订单服务进度通知");
				  listmsg.add( teacher.getNickname()+":有新的闪测订单！去抢单!"  );

				  listmsg.add(order.getOrderNo());
				  listmsg.add(list.get(0).getItemName());
				  listmsg.add(list.get(0).getDescription());

				  EventPublisher.publishEvent(new WxTemplateMsgEvent(this,  memberService.getById(teacher.getMemberId()).getOpenid(),templateId,url,listmsg));
			  }
	    	 

	       }

		// 释放锁
		redisUtils.remove(RedisConstant.REDISLOCK_KEY, o.getOrderNo() + ":" + o.getOrderNo() );

		redisUtils.remove(RedisConstant.REDISLOCK_KEY, o.getOrderNo() );
		

	}

	/**
	 * 提问订单
	 * 
	 * @param orderDoneEvent
	 */

	@Async
	@EventListener
	public void payaskServiceSuccess(OrderDoneEvent orderDoneEvent) {
		log.info(JSON.toJSONString(orderDoneEvent));
		log.info("订单支付成功监听");
		OrderBillEntity order = (OrderBillEntity) orderDoneEvent.getData();
		order = orderBillService.selectByorderForUpdate(order.getId());
		if (!order.getStatus().equals(0)) {
			log.info("该订单支付支付状态不符!");
			return;
		}
		OrderEntity o = orderService.selectOrderByNo(order.getOrderNo());
		o=orderService.selectOrderBYIdForUpdate(o.getId());
		// 订单类型，1-咨询订单，2-闪测订单，3-提问订单，4-充值订单
		if (!o.getServiceType().equals(OrderForm.orderType.ask.getType())) {
			log.info("该订单非提问订单退出!");
			return;
		}
	     Long memberID=o.getMemberId();
		Map<String, Object> map=  orderDoneEvent.getMap();
	       order.setTransNumber(map.get("transaction_id").toString());
	       order.setActualPrice(new BigDecimal(map.get("cash_fee").toString()).divide(new BigDecimal(100)));
	       order.setStatus(1);
	       orderBillService.updateById(order);
	       
	       o.setTotalAmount(order.getActualPrice());
	       o.setStatus(1);
	       orderService.updateById(o);
	 
	       List <OrderItemsEntity> list=     	orderItemsService.selectListByOrderNo(o.getId());
 
	       o.setItemlist(list);
	       o.setOrderBill(order);
	       
	       list.forEach(item->{

	    	MemberEntity m=	   memberService.selectMemberByIdForUpdate(memberID);
	    	m.setAskPeoplenum(m.getAskPeoplenum()+1);

	    	memberService.updateById(m);

	       });
	       

	     //  OrderCommissionEvent oc=new OrderCommissionEvent(this,o);
	       //EventPublisher.publishEvent(oc);
		o.setItemlist(list);
		o.setOrderBill(order);
		OrderCommissionEvent oc=new OrderCommissionEvent(this,o);
		EventPublisher.publishEvent(oc);
		
	       
	       
	       String templateId=businessConfigService.getBusinessConfigValue(null, "GZH_NOTE_TEMPLATE");
           BusinessEntity business=  businessService.getById( order.getBusinessId());
		  String url="wechat/topage?page=page/order/orderlist/orderlist&bcode="+business.getCode();
		  TeacherEntityDto teacher=		teacherService.selectById(o.getTeacherId());
         //MemberEntity member=		memberService.getById(order.getMemberId());
    	  List<String> listmsg=new ArrayList<>();

		listmsg.add("订单服务进度通知");
		listmsg.add( teacher.getNickname()+":您收到新的提问订单！"  );

		listmsg.add(order.getOrderNo());
		listmsg.add(list.get(0).getItemName());
		listmsg.add(list.get(0).getDescription());



		EventPublisher.publishEvent(new WxTemplateMsgEvent(this,  memberService.getById(teacher.getMemberId()).getOpenid(),templateId,url,listmsg));
		
		// 释放锁
		redisUtils.remove(RedisConstant.REDISLOCK_KEY, o.getOrderNo() + ":" + o.getOrderNo() );

		redisUtils.remove(RedisConstant.REDISLOCK_KEY, o.getOrderNo() );

	}

	/**
	 * 充值订单
	 * 
	 * @param orderDoneEvent
	 */

	@Async
	@EventListener
	public void payrechargeServiceSuccess(OrderDoneEvent orderDoneEvent) {
		log.info(JSON.toJSONString(orderDoneEvent));
		log.info("订单支付成功监听");
		OrderBillEntity order = (OrderBillEntity) orderDoneEvent.getData();
		order = orderBillService.selectByorderForUpdate(order.getId());
		if (!order.getStatus().equals(0)) {
			log.info("该订单支付支付状态不符!");
			return;
		}
		OrderEntity o = orderService.selectOrderByNo(order.getOrderNo());
		o=orderService.selectOrderBYIdForUpdate(o.getId());
		// 订单类型，1-咨询订单，2-闪测订单，3-提问订单，4-充值订单
		if (!o.getServiceType().equals(OrderForm.orderType.recharge.getType())) {
			log.info("该订单非充值订单 退出!");
			return;
		}
		
		Map<String, Object> map=  orderDoneEvent.getMap();
	       order.setTransNumber(map.get("transaction_id").toString());
	       order.setActualPrice(new BigDecimal(map.get("cash_fee").toString()).divide(new BigDecimal(100)));
	       order.setStatus(1);
	       orderBillService.updateById(order);
	       
	       o.setTotalAmount(order.getActualPrice());
	       o.setStatus(1);
	       orderService.updateById(o);
	       String commontent=order.getCommont();
	       Long id=order.getId();
	       Long memberID=o.getMemberId();
	       Long businessId=o.getBusinessId();
	       List <OrderItemsEntity> list=     	orderItemsService.selectListByOrderNo(o.getId());
	       list.forEach(item->{
			   RechargeEntity recharge=   rechargeService.getById(item.getItemId());
			   MemberWalletEntity mwall= memberWalletService.getByMemberId(memberID);
			   if(mwall==null){
				   mwall = new MemberWalletEntity();
				   mwall.setMemberId(memberID);
				   memberWalletService.save(mwall);
			   }
			   mwall=   memberWalletService.selectMemberWalletByIdFOrUpdate(mwall.getId());
			   mwall.setBalance(mwall.getBalance().add(recharge.getOriginalPrice()).add(recharge.getGiveMoney()));
			   mwall.setTotalBalance(mwall.getTotalBalance().add(recharge.getOriginalPrice()).add(recharge.getGiveMoney()));
			   mwall.setGiveBalance(mwall.getGiveBalance().add(recharge.getGiveMoney()));
			   mwall.setTotalGive(mwall.getTotalGive().add(recharge.getGiveMoney()));
			   memberWalletService.updateById(mwall);
			   MemberRechargeRecordEntity mrecgarg=new MemberRechargeRecordEntity();
			   mrecgarg.setBusinessId(businessId);
			   mrecgarg.setComment(commontent);
			   mrecgarg.setCreateTime(new Date());
			   mrecgarg.setGiverechargeMoney(recharge.getGiveMoney());
			   mrecgarg.setMemberId(memberID);
			   mrecgarg.setPayOrderId(id);
			   mrecgarg.setPayNo(map.get("transaction_id").toString());
			   mrecgarg.setRechargeBalance(mwall.getBalance());
			  // mrecgarg.setRechargeCode();
			   mrecgarg.setRechargeId(recharge.getId());
			   mrecgarg.setRechargeMoney(new BigDecimal(map.get("cash_fee").toString()).divide(new BigDecimal(100)));
			   mrecgarg.setStatus(1);
			   mrecgarg.setType(1);
			   memberRechargeRecordService.save(mrecgarg);
	       });

		log.info("充值订单 不分佣！");
		// 释放锁
		redisUtils.remove(RedisConstant.REDISLOCK_KEY, o.getOrderNo() + ":" + o.getOrderNo() );

		redisUtils.remove(RedisConstant.REDISLOCK_KEY, o.getOrderNo() );

	}



	/**
	 * 开通VIP订单
	 *
	 * @param orderDoneEvent
	 */

	@Async
	@EventListener
	public void payvipServiceSuccess(OrderDoneEvent orderDoneEvent) {
		log.info(JSON.toJSONString(orderDoneEvent));
		log.info("订单支付成功监听");
		OrderBillEntity order = (OrderBillEntity) orderDoneEvent.getData();
		order = orderBillService.selectByorderForUpdate(order.getId());
		if (!order.getStatus().equals(0)) {
			log.info("该订单支付支付状态不符!");
			return;
		}
		OrderEntity o = orderService.selectOrderByNo(order.getOrderNo());
		o=orderService.selectOrderBYIdForUpdate(o.getId());
		// 订单类型，1-咨询订单，2-闪测订单，3-提问订单，4-充值订单
		if (!o.getServiceType().equals(OrderForm.orderType.member_cert.getType())) {
			log.info("该订单非VIP认证订单 退出!");
			return;
		}

		Map<String, Object> map=  orderDoneEvent.getMap();
		order.setTransNumber(map.get("transaction_id").toString());
		order.setActualPrice(new BigDecimal(map.get("cash_fee").toString()).divide(new BigDecimal(100)));
		order.setStatus(1);
		orderBillService.updateById(order);

		o.setTotalAmount(order.getActualPrice());
		o.setStatus(3);
		orderService.updateById(o);
		String commontent=order.getCommont();
		Long id=order.getId();

		MemberEntity memberEntity = memberService.selectMemberByIdForUpdate(order.getMemberId());

		memberEntity.setMemberLevel(1);
		memberService.updateById(memberEntity);
		memberService.removeMemberCache(memberEntity);


		List <OrderItemsEntity> list=     	orderItemsService.selectListByOrderNo(o.getId());
		o.setItemlist(list);
		o.setOrderBill(order);
		OrderCommissionEvent oc=new OrderCommissionEvent(this,o);
		EventPublisher.publishEvent(oc);

		log.info("充值VIP订单 分佣！");
		// 释放锁
		redisUtils.remove(RedisConstant.REDISLOCK_KEY, o.getOrderNo() + ":" + o.getOrderNo() );

		redisUtils.remove(RedisConstant.REDISLOCK_KEY, o.getOrderNo() );

	}



	/**
	 * 余额支付订单 释放分布式设备锁，更新订单日志 更新订单信息 ,开锁 ，发送短信
	 *
	 * @param balancePayDoneEvent
	 */
	@Async
	@EventListener
	public void balancePayServiceSuccess(BalancePayDoneEvent balancePayDoneEvent) {
		log.info(JSON.toJSONString(balancePayDoneEvent));
		log.info("余额支付订单成功监听");
		OrderBillEntity order = (OrderBillEntity) balancePayDoneEvent.getData();
		order = orderBillService.selectByorderForUpdate(order.getId());
		if (!order.getStatus().equals(0)) {
			log.info("该订单支付支付状态不符!");
			return;
		}
		OrderEntity o = orderService.selectOrderByNo(order.getOrderNo());
		o=orderService.selectOrderBYIdForUpdate(o.getId());
		// 订单类型，1-咨询订单，2-闪测订单，3-提问订单，4-充值订单
		if (o ==null||!o.getServiceType().equals(OrderForm.orderType.consult.getType())) {
			log.info("非订单咨询订单 退出!");
			return;
		}
		Map<String, Object> map=  balancePayDoneEvent.getMap();
		order.setTransNumber(map.get("transaction_id").toString());
		order.setActualPrice(new BigDecimal(map.get("cash_fee").toString()).divide(new BigDecimal(100)));
		order.setStatus(1);
		orderBillService.updateById(order);

		o.setTotalAmount(order.getActualPrice());
		o.setStatus(1);
		orderService.updateById(o);

		List <OrderItemsEntity> list=     	orderItemsService.selectListByOrderNo(o.getId());
		list.forEach(item->{
			TeacherServiceEntity tearchService = teacherServiceService.selectTezcherServiceForUpdate(item.getItemId());
			tearchService.setTestPeoplenum((tearchService.getTestPeoplenum()==null?0:tearchService.getTestPeoplenum())+1);
			teacherServiceService.updateById(tearchService);
		});
		o.setItemlist(list);
		o.setOrderBill(order);
		OrderCommissionEvent oc=new OrderCommissionEvent(this,o);
		EventPublisher.publishEvent(oc);

		/**
		 * {{first.DATA}}
		 * 服务单号：{{keyword1.DATA}}
		 * 咨询主题：{{keyword2.DATA}}
		 * 咨询内容：{{keyword3.DATA}}
		 * {{remark.DATA}}
		 */
		//发送公众号模板消息
		String templateId=businessConfigService.getBusinessConfigValue(null, "GZH_NOTE_TEMPLATE");
		BusinessEntity business=  businessService.getById( order.getBusinessId());
		String url="wechat/topage?page=page/order/orderlist/orderlist&bcode="+business.getCode();
		TeacherEntityDto teacher=		teacherService.selectById(o.getTeacherId());
		// MemberEntity member=		memberService.getById(order.getMemberId());
		List<String> listmsg=new ArrayList<>();

		listmsg.add("订单服务进度通知");
		listmsg.add( teacher.getNickname()+":您收到新的咨询订单！"  );

		listmsg.add(order.getOrderNo());
		listmsg.add(list.get(0).getItemName());
		listmsg.add(list.get(0).getDescription());



		EventPublisher.publishEvent(new WxTemplateMsgEvent(this,  memberService.getById(teacher.getMemberId()).getOpenid(),templateId,url,listmsg));

		// 释放锁
		redisUtils.remove(RedisConstant.REDISLOCK_KEY, o.getOrderNo() + ":" + o.getOrderNo() );

		redisUtils.remove(RedisConstant.REDISLOCK_KEY, o.getOrderNo() );

	}
	/**
	 * 余额支付订单 释放分布式设备锁，更新订单日志 更新订单信息 ,开锁 ，发送短信
	 *
	 * @param balancePayDoneEvent
	 */
	@Async
	@EventListener
	public void balancePayQuickServiceSuccess(BalancePayDoneEvent balancePayDoneEvent) {
		log.info(JSON.toJSONString(balancePayDoneEvent));
		log.info("订单支付成功监听");
		OrderBillEntity order = (OrderBillEntity) balancePayDoneEvent.getData();
		order = orderBillService.selectByorderForUpdate(order.getId());
		if (!order.getStatus().equals(0)) {
			log.info("该订单支付支付状态不符!");
			return;
		}
		OrderEntity o = orderService.selectOrderByNo(order.getOrderNo());
		o=orderService.selectOrderBYIdForUpdate(o.getId());
		// 订单类型，1-咨询订单，2-闪测订单，3-提问订单，4-充值订单
		if (!o.getServiceType().equals(OrderForm.orderType.quick.getType())) {
			log.info("非闪测订单，退出!");
			return;
		}
		Map<String, Object> map=  balancePayDoneEvent.getMap();
		order.setTransNumber(map.get("transaction_id").toString());
		order.setActualPrice(new BigDecimal(map.get("cash_fee").toString()).divide(new BigDecimal(100)));
		order.setStatus(1);
		orderBillService.updateById(order);

		o.setTotalAmount(order.getActualPrice());
		o.setStatus(4);
		orderService.updateById(o);

		List <OrderItemsEntity> list=     	orderItemsService.selectListByOrderNo(o.getId());
		list.forEach(item->{

			QuicktestEntity quick=  quicktestService.selectQuickTestByidForUPdate(item.getItemId());
			quick.setTestPeoplenum((quick.getTestPeoplenum()==null?0:quick.getTestPeoplenum())+1);
			quicktestService.updateById(quick);
		});
		o.setItemlist(list);
		o.setOrderBill(order);
		OrderCommissionEvent oc=new OrderCommissionEvent(this,o);
		EventPublisher.publishEvent(oc);


		List <TeacherEntity>teacherlist=  teacherService.selectTeachersByJoinQuickAsk();
		BusinessEntity business=  businessService.getById( order.getBusinessId());
		if(teacherlist!=null&&!teacherlist.isEmpty())
			for (TeacherEntity teacherEntity : teacherlist) {
				if(!o.getMemberId().equals(teacherEntity.getMemberId())){
					String templateId=businessConfigService.getBusinessConfigValue(null, "GZH_NOTE_TEMPLATE");
					String url="wechat/topage?page=page/teacher/teacherindex/teacherindex&bcode="+business.getCode();
					TeacherEntityDto teacher=		teacherService.selectById(teacherEntity.getId());
					//	MemberEntity member=		memberService.getById(order.getMemberId());
					List<String> listmsg=new ArrayList<>();

					listmsg.add("订单服务进度通知");
					listmsg.add( teacher.getNickname()+":有新的闪测订单！去抢单!"  );

					listmsg.add(order.getOrderNo());
					listmsg.add(list.get(0).getItemName());
					listmsg.add(list.get(0).getDescription());

					EventPublisher.publishEvent(new WxTemplateMsgEvent(this,  memberService.getById(teacher.getMemberId()).getOpenid(),templateId,url,listmsg));
				}


			}

		// 释放锁
		redisUtils.remove(RedisConstant.REDISLOCK_KEY, o.getOrderNo() + ":" + o.getOrderNo() );

		redisUtils.remove(RedisConstant.REDISLOCK_KEY, o.getOrderNo() );
	}


	/**
	 * 开通VIP订单
	 *
	 * @param balancePayDoneEvent
	 */

	@Async
	@EventListener
	public void balancePayVipSuccess(BalancePayDoneEvent balancePayDoneEvent) {
		log.info(JSON.toJSONString(balancePayDoneEvent));
		log.info("余额支付开通VIP成功监听");
		OrderBillEntity order = (OrderBillEntity) balancePayDoneEvent.getData();
		order = orderBillService.selectByorderForUpdate(order.getId());
		if (!order.getStatus().equals(0)) {
			log.info("该订单支付支付状态不符!");
			return;
		}
		OrderEntity o = orderService.selectOrderByNo(order.getOrderNo());
		o=orderService.selectOrderBYIdForUpdate(o.getId());
		// 订单类型，1-咨询订单，2-闪测订单，3-提问订单，4-充值订单
		if (!o.getServiceType().equals(OrderForm.orderType.member_cert.getType())) {
			log.info("该订单非VIP认证订单,退出!");
			return;
		}

		Map<String, Object> map=  balancePayDoneEvent.getMap();
		order.setTransNumber(map.get("transaction_id").toString());
		order.setActualPrice(new BigDecimal(map.get("cash_fee").toString()).divide(new BigDecimal(100)));
		order.setStatus(3);
		orderBillService.updateById(order);

		o.setTotalAmount(order.getActualPrice());
		o.setStatus(1);
		orderService.updateById(o);
		String commontent=order.getCommont();
		Long id=order.getId();

		MemberEntity memberEntity = memberService.selectMemberByIdForUpdate(order.getMemberId());

		memberEntity.setMemberLevel(1);
		memberService.updateById(memberEntity);
		memberService.removeMemberCache(memberEntity);


		List <OrderItemsEntity> list=     	orderItemsService.selectListByOrderNo(o.getId());
		o.setItemlist(list);
		o.setOrderBill(order);
		OrderCommissionEvent oc=new OrderCommissionEvent(this,o);
		EventPublisher.publishEvent(oc);

		log.info("充值VIP订单，分佣！");
		// 释放锁
		redisUtils.remove(RedisConstant.REDISLOCK_KEY, o.getOrderNo() + ":" + o.getOrderNo() );

		redisUtils.remove(RedisConstant.REDISLOCK_KEY, o.getOrderNo() );

	}
}
